﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;

namespace myDNS
{
   public class HostName_IP_Port
    {
        public string _hostName;
        public string _ip;
        public int _port;

        public HostName_IP_Port()
        {
            _hostName = " No Host Name ";
            _ip = "0.0.0.0";
            _port = 0;

        }
        public HostName_IP_Port(string hostName, string ip, int port)
        {
            _hostName = hostName;
            _ip = ip;
            _port = port;
        }

    }

 public   class Message
    {
        public int _type; // 0 = send , 1 = receive
        public string _content;

        public Message()
        {
            _type = -1;
            _content = "";
        }
    }

  public  class Provider
    {
        public static byte[] GetBytes(string str)
        {
            byte[] bytes = new byte[str.Length * sizeof(char)];
            System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
            return bytes;
        }

        public static string GetString(byte[] bytes)
        {
            char[] chars = new char[bytes.Length / sizeof(char)];
            System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
            return new string(chars);
        }
       
        public static string GetStringCompare(string strInput, int level)
        {
            // mail . yahoo . com level 1 => .com  level 2 => yahoo.com  level 3 => mail.yahoo.com  level 4 .mail.yahoo.com
            //  0      1       2   
            //              com  level 4 
            string kq = "";
            string[] strArray = strInput.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
            int count = 0;
            int index = strArray.Length - 1;
            while (count < level)
            {
                if (index < 0)
                    break;
                kq = strArray[index] + kq;
                if ((count + 1) <level)
                {
                    kq = "." + kq;
                }
                
                count++;
                index--;
            }
            return kq;
        }
        public static HostName_IP_Port GetHostName_IP_Port(byte[] data)
        {
            HostName_IP_Port kq = new HostName_IP_Port();
            string strData = GetString(data);
            strData = strData.Substring(1, strData.Length - 1);
            string[] dataArray = strData.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            kq._ip = dataArray[0];
            kq._port = int.Parse(dataArray[1].ToString());
            return kq;
        }
        public static string SendIpPort(string ip, int port, string data)
        {
            string kq = "";
            kq = ip + "," + port.ToString() + "," + data;
            return kq;
        }
        public static HostName_IP_Port GetHostName_IP_Port(string sendData)
        {
            HostName_IP_Port kq = new HostName_IP_Port();
            string[] dataArray = sendData.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            kq._ip = dataArray[0].ToString();
            kq._port = int.Parse(dataArray[1].ToString());
            kq._hostName = dataArray[2].ToString();
            return kq;
        }

    }
 
  public  class DNSServer
    {

        public string _nameServer;
        public string _ip;
        public int _port;

        public DNSServer(string nameServer, string ip, int port)
        {
            _nameServer = nameServer;
            _ip = ip;
            _port = port;
        }
        public void ToString()
        {
            Console.WriteLine(" DNS server " + _nameServer + "listen : ");
            Console.WriteLine(" IP : " + _ip);
            Console.WriteLine(" Port : " + _port);
        }

        public HostName_IP_Port SearchHostName(string hostName, string fileTextDNS)
        {
            HostName_IP_Port kq = new HostName_IP_Port();
            StreamReader reader = new StreamReader(fileTextDNS);
            string result = reader.ReadLine();
            while (result != null)
            {
                string[] str = result.Split(new char[] { ' ','\t' }, StringSplitOptions.RemoveEmptyEntries);
                str[0] = str[0].ToLower();

                if (str[0].CompareTo(hostName) == 0)
                {
                    kq._hostName = str[0].ToString();
                    kq._ip = str[1].ToString();
                    kq._port = int.Parse(str[2].ToString());
                    break;
                }
                result = reader.ReadLine();
            }
            reader.Close();
            return kq;
        }

        public void SendToServer(DNSServer server, Message _message)
        {
            string strSend = _message._type.ToString() + _message._content.ToString();
            byte[] byteSend = Provider.GetBytes(strSend);
            UdpClient sock;   
            string ip = server._ip;
            int port = server._port;
            try
            {
                sock = new UdpClient();
                sock.Connect(ip, port);              
                sock.Send(byteSend, byteSend.Length);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        public void ReceiveFromSerser()
        {

                //UdpClient sock;
                //int port = 89;
                //IPEndPoint ep;
                //try
                //{
                //    sock = new UdpClient(port);
                //    ep = new IPEndPoint(0, 89);
                //    do
                //    {
                //    byte[] recData = sock.Receive(ref ep);
                        

                //        byte[] macAdd = new byte[6];
                //        for (int i = 0; i < 6; i++)
                //        {
                //            macAdd[i] = recData[i];
                //        }

                //        byte[] temp = new byte[recData.Length - 6];


                //        for (int i = 0; i < recData.Length - 6; i++)
                //        {
                //            temp[i] = recData[i + 6];
                //        }


                //        string macAddress = ByteArrayToString(macAdd);
                //        Console.WriteLine("MacAddress : " + macAddress);
                //        Console.WriteLine("Data:" + Encoding.Default.GetString(temp));
                //    }
                //    while (true);
                //}
                //catch (Exception ex)
                //{
                //    Console.Write(ex.Message.ToString());
                //}

            
            
        }


    }
}
